##################################################################################################################################
#
# Copyright (C) 2008-2013 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################

proc add_product {} {
	set values [$::daidze_main get_values]
	set msg_list [dict create name "Product name is obligatory." default_price "Default price is obligatory." fk_category "Default category is obligatory." fk_tax "Default tax is obligatory." fk_supplier "Supplier name is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		set cat_id [$::db select_id_by_name category [dict get $values fk_category]]
		set prod_id [expr [$::db select_last_inserted product id] + 1]
		
		set val_to_ins [dict remove $values gen_barc photo]
		if {[dict get $values gen_barc] != 0} {
			dict set val_to_ins barcode "$cat_id$prod_id"
		}
		# puts $val_to_ins
		if {[dict get $values photo] ne ""} {
			set img_handler [dict get $values photo]
			
			set tmp_doc [::fileutil::tempfile]
			$img_handler write $tmp_doc
			set rf [open $tmp_doc r]
			fconfigure $rf -translation binary
			set file_data [read $rf]
			dict set val_to_ins image $file_data
		}

		$::db insert_all product $val_to_ins
		$::daidze_main load_main product product.xml $::main 1
	}
}


proc update_image_preview {w} {
	set img_path [$w get]
	if {$img_path ne ""} {
		image create photo img -file $img_path
		set d [resize img 300 200]
		$::daidze_main update_form [dict create photo $d]
	}
}

proc edit_product {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]
	set product_var {id name default_price default_price_with_tax fk_category fk_tax}
	if {[$my_tablelist curselection] ne ""} {
		set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
		foreach key $product_var value $row {
			dict set sdata $key $value
		}
		set stock_qty [$::db select_all product {stock_qty_min stock_qty_max barcode fk_supplier visible image} product.id=\'[dict get $sdata id]\' list]
		dict set sdata stock_qty_min [lindex $stock_qty 0]
		dict set sdata stock_qty_max [lindex $stock_qty 1]
		dict set sdata barcode [lindex $stock_qty 2]
		dict set sdata fk_supplier [lindex $stock_qty 3]
		dict set sdata visible [lindex $stock_qty 4]
		set img_data [lindex $stock_qty 5]
		if { $img_data ne ""} {
			set pr_img [image create photo -data [pg_unescape_bytea $img_data]]
			dict set sdata photo $pr_img
		}
		$::daidze_main load_main product edit_product.xml $::main
		$::daidze_main update_form $sdata
	} else {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select entry to edit."]"
	}
}

proc update_product {} {
	set values [$::daidze_main get_values]

	set msg_list [dict create name "Product name is obligatory." default_price "Default price is obligatory." fk_category "Default category is obligatory." fk_tax "Default tax is obligatory." fk_supplier "Supplier name is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		$::db transaction start
		set cat_id [$::db select_id_by_name category [dict get $values fk_category]]
		set prod_id [dict get $values id]
		if {[dict get $values gen_barc] != 0} {
			dict set values barcode "$cat_id$prod_id"
		}
		set val_to_ins [dict remove $values gen_barc photo]
		
		if {[dict get $values photo] ne ""} {
			set img_handler [dict get $values photo]
			
			set tmp_doc [::fileutil::tempfile]
			$img_handler write $tmp_doc
			set rf [open $tmp_doc r]
			fconfigure $rf -translation binary
			set file_data [read $rf]
			dict set val_to_ins image $file_data
		}
		$::db update_all product $val_to_ins
		$::db transaction stop
		$::daidze_main load_main product product.xml $::main 1
	}
}

proc update_price_with_tax {w} {
	set price_without_tax [$w get]
	if {$price_without_tax ne ""} {
		set tax_name [$::daidze_main get_values list fk_tax]
		if {$tax_name ne ""} {
			set price_with_tax [lindex [::pos::value_plus_tax $price_without_tax $tax_name] 0]
		} else {
			set price_with_tax $price_without_tax
		}
		$::daidze_main update_form [dict create default_price_with_tax [round $price_with_tax]]
	} else {
		set price_with_tax ""
		$::daidze_main update_form [dict create default_price_with_tax $price_with_tax]
	}
	
}

proc update_price_without_tax {w} {
	set price_with_tax [$w get]
	if {$price_with_tax ne ""} {
		set tax_name [$::daidze_main get_values list fk_tax]
		if {$tax_name ne ""} {
			set price_without_tax [lindex [::pos::value_minus_tax $price_with_tax $tax_name] 0]
		} else {
			set price_without_tax $price_with_tax
		}
		$::daidze_main update_form [dict create default_price [round $price_without_tax]]
	} else {
		set price_without_tax ""
		$::daidze_main update_form [dict create default_price $price_without_tax]
	}

}

proc update_prices {w} {
	set tax_name [$w get]
	set stype [$w get]
	if {$stype ne ""} {
		$::daidze_main state_widget {default_price default_price_with_tax} enabled
	} else {
		$::daidze_main state_widget {default_price default_price_with_tax} disabled 1
	}
	set price_with_tax [lindex [$::daidze_main get_values list default_price_with_tax] 0]
	set price_without_tax [lindex [$::daidze_main get_values list default_price] 0]
	if {$price_with_tax ne ""} {
		$::daidze_main update_form [dict create default_price [lindex [::pos::value_minus_tax $price_with_tax $tax_name] 0]]
	} elseif {$price_without_tax ne ""} {
		$::daidze_main update_form [dict create default_price_with_tax [lindex [::pos::value_plus_tax $price_without_tax $tax_name] 0]]
	}
	
}


proc print_barcodes {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]
		if {[$my_tablelist curselection] ne ""} {
		set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
		set product_name [lindex $row 1]
		set res [$::db select_all product barcode "name=\'$product_name\'" list]
		dict set sdata barcode [lindex $res 0]
		if {[dict get $sdata barcode] != ""} {
			dict set sdata name [lindex $row 1]
			dict set sdata price [lindex $row 3]
			set barcode_form [top_win "Print barcodes"]
			set print_barcodes_frame [ttk::frame $barcode_form.top]
			set ::daidze_pr_barc [xml_wrapper new]
			pack $print_barcodes_frame
			
			$::daidze_pr_barc load_main product print_barcodes_form.xml $print_barcodes_frame
			$::daidze_pr_barc update_form $sdata
		} else {
			tk_messageBox -icon info -type ok -message "[::msgcat::mc "There is no barcode for this product."]"
		}
	} else {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select entry please."]"
	}
}

proc send_barcodes_to_printer {} {
	set values [$::daidze_pr_barc get_values]
	
	set msg_list [dict create qty "Quantaty field is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		set bc [String2C128 [code128checksum [dict get $values barcode]]]
		set im [c128img $bc]
		set start_y 0
		set hdc [lindex [printer open] 0]
		printer job start -name gdi
		printer page start
		for {set round 0} {$round < [dict get $values qty]} {incr round} {
			gdi text $hdc 220 $start_y -text [dict get $values name] -font {tahoma 8} -anchor e
			set start_y [expr $start_y + 20]
			gdi text $hdc 240 $start_y -text [dict get $values price] -font {tahoma 8} -anchor e
			set start_y [expr $start_y + 20]
			gdi photo $hdc -destination "0 $start_y 280 160" -photo $im
			set start_y [expr $start_y + 250]
		}
		printer page end
		printer job end
		printer close
	}
	destroy .topwin
}

proc img_preview {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]
	if {[$my_tablelist curselection] ne ""} {
		set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
		set product_id [lindex $row 0]
		set esc_img [lindex [$::db select_all product image "id='$product_id'" list] 0]
		set s [image create photo -data [pg_unescape_bytea $esc_img]]
		image_viewer $s
	} else {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select product to preview."]"
	}
}